package GUILayer;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import ControlLayer.OrderCtr;
import ControlLayer.ProductCtr;
import ModelLayer.Clothing;
import ModelLayer.Customer;
import ModelLayer.SalesOrder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProductClothingMenu {
	private ProductClothingMenu instance;
	
	public JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField txtId;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProductClothingMenu window = new ProductClothingMenu();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ProductClothingMenu() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(43, 22, 46, 14);
		frame.getContentPane().add(lblName);
		
		JLabel lblPurchasePrice = new JLabel("Purchase Price:");
		lblPurchasePrice.setBounds(43, 52, 81, 14);
		frame.getContentPane().add(lblPurchasePrice);
		
		JLabel lblNewLabel = new JLabel("Sales price:");
		lblNewLabel.setBounds(43, 77, 81, 14);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblRentPrice = new JLabel("Rent price:");
		lblRentPrice.setBounds(43, 102, 81, 14);
		frame.getContentPane().add(lblRentPrice);
		
		JLabel lblCountryOfOrigin = new JLabel("Country of origin:");
		lblCountryOfOrigin.setBounds(43, 127, 93, 14);
		frame.getContentPane().add(lblCountryOfOrigin);
		
		JLabel lblStock = new JLabel("Stock:");
		lblStock.setBounds(43, 152, 46, 14);
		frame.getContentPane().add(lblStock);
		
		JLabel lblSupplierId = new JLabel("Supplier ID:");
		lblSupplierId.setBounds(43, 177, 81, 14);
		frame.getContentPane().add(lblSupplierId);
		
		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				insertProductClothing(e);
			}
		});
		btnCreate.setBounds(43, 216, 89, 23);
		frame.getContentPane().add(btnCreate);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
			}
		});
		btnCancel.setBounds(279, 216, 89, 23);
		frame.getContentPane().add(btnCancel);
		
		textField = new JTextField();
		textField.setBounds(147, 19, 129, 20);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(147, 49, 66, 20);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(147, 74, 66, 20);
		frame.getContentPane().add(textField_2);
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setBounds(147, 99, 66, 20);
		frame.getContentPane().add(textField_3);
		textField_3.setColumns(10);
		
		textField_4 = new JTextField();
		textField_4.setBounds(147, 124, 129, 20);
		frame.getContentPane().add(textField_4);
		textField_4.setColumns(10);
		
		textField_5 = new JTextField();
		textField_5.setBounds(147, 149, 66, 20);
		frame.getContentPane().add(textField_5);
		textField_5.setColumns(10);
		
		textField_6 = new JTextField();
		textField_6.setBounds(147, 174, 66, 20);
		frame.getContentPane().add(textField_6);
		textField_6.setColumns(10);
		
		JLabel lblColor = new JLabel("Color:");
		lblColor.setBounds(230, 52, 46, 14);
		frame.getContentPane().add(lblColor);
		
		JLabel lblSize = new JLabel("Size:");
		lblSize.setBounds(230, 77, 46, 14);
		frame.getContentPane().add(lblSize);
		
		textField_7 = new JTextField();
		textField_7.setBounds(279, 49, 86, 20);
		frame.getContentPane().add(textField_7);
		textField_7.setColumns(10);
		
		textField_8 = new JTextField();
		textField_8.setBounds(279, 74, 86, 20);
		frame.getContentPane().add(textField_8);
		textField_8.setColumns(10);
		
		txtId = new JTextField();
		txtId.setBounds(279, 174, 86, 20);
		frame.getContentPane().add(txtId);
		txtId.setColumns(10);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchProductClothing(e);
			}
		});
		btnSearch.setBounds(142, 216, 89, 23);
		frame.getContentPane().add(btnSearch);
	}

	private void searchProductClothing(java.awt.event.ActionEvent evt)
	{
	       ProductCtr productCtr = new ProductCtr();
	       String input = txtId.getText();
	       System.out.println(input);
	       
	       //search by ID
	       Clothing cloth = productCtr.searchClothingByID(Integer.parseInt(input));
	       if(String.valueOf(cloth.getID()).equals(""))
	       {
	    	   JOptionPane.showMessageDialog(null, "The inserted order ID does not exists");
	       }
	       else
	       {
	    	   clearFields();
	           putValuesOnTheScreen(cloth);
	       }
	}
	
	private void insertProductClothing(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertEmp
        ProductCtr productCtr = new ProductCtr();
  
        String name = textField.getText();
        int suppID = Integer.parseInt(textField_6.getText());
        float purchasePrice = Float.parseFloat(textField_1.getText());
        float salesPrice = Float.parseFloat(textField_2.getText());
        float rentPrice = Float.parseFloat(textField_3.getText());
        String countryOfOrigin = textField_4.getText();
        int minStock = Integer.parseInt(textField_5.getText());
        String newColour = textField_7.getText();
        String newSize = textField_8.getText();        
        
        productCtr.insertClothing(name, suppID, purchasePrice, salesPrice, rentPrice, countryOfOrigin, minStock, newColour, newSize);
    }
	
    private void clearFields()
    {
          textField.setText("");
          textField_1.setText("");
          textField_2.setText("");
          textField_3.setText("");
          textField_4.setText("");
          textField_5.setText("");
          textField_6.setText("");
          textField_7.setText("");
          textField_8.setText("");
          txtId.setText("");          
    }
    
    private void putValuesOnTheScreen(Clothing cloth)
    {    	
    	  textField.setText(cloth.getName());
          textField_1.setText(String.valueOf(cloth.getPurchasePrice()));
          textField_2.setText(String.valueOf(cloth.getSalesPrice()));
          textField_3.setText(String.valueOf(cloth.getRentPrice()));
          textField_4.setText(cloth.getCountryOfOrigin());
          textField_5.setText(String.valueOf(cloth.getMinStock()));
          textField_6.setText(String.valueOf(cloth.getSupplier().getId()));
          textField_7.setText(cloth.getColour());
          textField_8.setText(cloth.getSize());
          txtId.setText(String.valueOf(cloth.getID())); 
    }
}
